package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.UserState;
import com.itheima.service.db.UserStateService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author: Y F
 * @create: 2022-09-07 17:32
 */
@DubboService
public class UserStateServiceImpl implements UserStateService {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 保存状态对象
     * @param userState
     */
    @Override
    public void save(UserState userState) {
        mongoTemplate.save(userState);
    }

    /**
     * 通过id获取
     * @param userId
     * @return
     */
    @Override
    public UserState findByUserId(long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );

        return mongoTemplate.findOne(query, UserState.class);
    }
}
